package com.zlzlib.libdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * Created by zlz on 2019/9/18.
 * Binding 弹窗基本类
 */
public abstract class BaseDialog<B extends ViewDataBinding> extends Dialog {

    protected B mBinding;
    protected Context context;
    /**
     * (在给定时间后,自动消失)
     */
    private boolean mAutoDismiss = false;
    /**
     * (对话框消失延时时间,毫秒值)
     */
    private long mAutoDismissDelay = 1500;

    protected abstract int getContentViewId();

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initBinding();
        initView();
    }

    public BaseDialog(Context context) {
        this(context, R.style.Dialog);
    }

    /**
     * (当dialog依附在window上 )
     */
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        delayDismiss();
    }

    private void initBinding() {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), getContentViewId(), null, false);
        setContentView(mBinding.getRoot());
    }

    /**
     * 在继承的构造函数super时执行  注意使用的初始化数据可能为空
     */
    protected void initView() {
    }

    /**
     * 设置弹窗宽高
     *
     * @param w 宽与屏幕的比例
     * @param h 高与屏幕的比例
     */
    protected void setDialogWH(double w, double h) {
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
        if (w > 0) {
            params.width = (int) (dm.widthPixels * w);
        }
        if (h > 0) {
            params.height = (int) (dm.heightPixels * h);
        }
    }

    protected Point getScreenResolution(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point screenResolution = new Point();
        display.getSize(screenResolution);
        return screenResolution;
    }

    /**
     * 是否为竖屏
     */
    protected boolean isPortrait(Context context) {
        Point screenResolution = getScreenResolution(context);
        return screenResolution.y > screenResolution.x;
    }

    /**
     * 设置位置为底部居中
     */
    protected void setLocBottom() {
        this.getWindow().setWindowAnimations(R.style.AnimBottom);
        this.getWindow().setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    }

    /**
     * 设置位置为顶部居中
     */
    protected void setLocTop() {
        this.getWindow().setWindowAnimations(R.style.AnimTop);
        this.getWindow().setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    }

    /**
     * 设置位置为左部居中
     */
    protected void setLocLeft() {
        this.getWindow().setWindowAnimations(R.style.AnimLeft);
        this.getWindow().setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    }

    /**
     * 设置位置为右部居中
     */
    protected void setLocRight() {
        this.getWindow().setWindowAnimations(R.style.AnimRight);
        this.getWindow().setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    }

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private void delayDismiss() {
        if (mAutoDismiss && mAutoDismissDelay > 0) {
            mHandler.postDelayed(this::dismiss, mAutoDismissDelay);
        }
    }

    /**
     * @param mAutoDismiss 设置是否自动消失
     */
    public void setAutoDismiss(boolean mAutoDismiss) {
        this.mAutoDismiss = mAutoDismiss;
    }

    /**
     * @param mAutoDismissDelay 设置自动消失时间 单位秒
     */
    public void setAutoDismissDelay(long mAutoDismissDelay) {
        this.mAutoDismissDelay = mAutoDismissDelay;
    }
}
